﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class LoginMaster : System.Web.UI.Page
{
    EmployeeMethord EM = new EmployeeMethord();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            panlChangePassword.Visible = false;
        }
    }
    protected void btnLoginMaster_Click(object sender, EventArgs e)
    {
        string TypeLogin = "";
        if (rbtnAdmin.Checked)
        {
            TypeLogin = "Admin";
        }
        else if (rbtnHR.Checked)
        {
            TypeLogin = "HR";
        }
        else
        {
            TypeLogin = "Interviewer";
        }
        if (btnLoginMaster.Text.Equals("Login"))
        {
            bool Result = EM.CheckLogin(txtUserName.Text, txtPass.Text, TypeLogin);
            if (Result)
            {
                if (txtPass.Text.Equals("123456"))
                {
                    panlChangePassword.Visible = true;
                    txtUserName.Enabled = false;
                    rbtnAdmin.Enabled = false;
                    rbtnHR.Enabled = false;
                    rbtnInterview.Enabled = false;
                    btnLoginMaster.Text = "Change Password";
                    return;
                }
                else
                {
                    Session["User"] = txtUserName.Text;
                    switch (TypeLogin)
                    {
                        case "Interviewer":
                            Response.Redirect("Interviewer/Home.aspx");
                            break;
                        case "HR":
                            Response.Redirect("Employee/Home.aspx");
                            break;
                        default:
                            Response.Redirect("Admin/Home.aspx");
                            break;
                    }
                }
            }
            else
            {
                lblError.Text = "UserName or Password Invalid";
            }
        }
        else
        {
            if (txtPass.Text.Equals(""))
            {
                lblError.Text = "Please complete the information";
            }
            else if (txtNewpassword.Text.Equals(""))
            {
                lblError.Text = "Please complete the infomation";
            }
            else if(txtPasswordConfirm.Text.Equals(""))
            {
                lblError.Text = "Please complete the information";
            }
            else if (!txtNewpassword.Text.Equals(txtPasswordConfirm.Text))
            {
                lblError.Text = "Password confirm invalid";
            }
            else if (!txtPass.Text.Equals("123456"))
            {
                lblError.Text = "Password wrong.";
            }
            else
            {
                int check = EM.UpdatePassword(txtUserName.Text, txtNewpassword.Text, TypeLogin);
                if (check > 0)
                {
                    Session["User"] = txtUserName.Text;
                    Response.Write("<script type='text/javascript'>alert('Change password successful.');</script>");
                    switch (TypeLogin)
                    {
                        case "Interviewer":
                            Response.Redirect("Interviewer/Home.aspx");
                            break;
                        case "HR":
                            Response.Redirect("Employee/Home.aspx");
                            break;
                        default:
                            Response.Redirect("Admin/Home.aspx");
                            break;
                    }
                }
                else
                {
                    lblError.Text = "Change password unsuccessful";
                }
            }
        }
    }
}